﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDemo._02_对象和类型
{
    class Method
    {

        public static void Test()
        {
            StaticMethod();

            int[] arr = { 1, 2, 3, 4, 5 };
            int i = 1;
            TestValuePass(arr, i);
            Console.WriteLine("\n引用传递和值传递的不同");
            Console.WriteLine("arr[0]=" + arr[0]);
            Console.WriteLine("i=" + i);

            TestRef(arr, ref  i);
            Console.WriteLine("\n使用ref参数");
            Console.WriteLine("arr[0]=" + arr[0]);
            Console.WriteLine("i=" + i);

            string testOut;
            TestOut(out testOut);
            Console.WriteLine("\n使用out参数");
            Console.WriteLine(testOut);

            Console.WriteLine("\n使用命名参数");
            string FirstName = "MyFistName";
            string LastName = "MyLastName";
            TestName(LastName: LastName, FirstName: FirstName);

            Console.WriteLine("\n方法的重载");
            Console.WriteLine("add(1,2)=" + add(1, 2));
            Console.WriteLine("add(1,2,3)=" + add(1, 2, 3));

            Console.WriteLine("\n可选参数");
            GetName(FirstName);
        }

        //1.静态方法
        public static void StaticMethod()
        {
            Console.WriteLine("这是一个静态方法");
        }

        //2.按引用传递和按值传递的不同.
        public static void TestValuePass(int[] arr, int i)
        {
            arr[0] = 100;
            i = 100;
        }

        //3.ref参数 ， 强迫按引用传递参数
        public static void TestRef(int[] arr, ref  int i)
        {
            arr[0] = 1000;
            i = 1000;
        }

        //4.out参数，传递给方法的参数不用初始化
        public static void TestOut(out   string str)
        {
            str = "使用out， 不需要给方法初始值";
        }

        //5.命名参数
        public static void TestName(string FirstName, string LastName)
        {
            Console.WriteLine("FirstName:" + FirstName + "\nLastName:" + LastName);
        }

        //6.方法的重载
        public static int add(int a, int b)
        {
            return a + b;
        }

        public static int add(int a, int b, int c)
        {
            return a + b + c;
        }

        //7.可选参数
        public static void GetName(string FirstName, string LastName = "optionalName")
        {
            Console.WriteLine("FistName=" + FirstName + "\nLastName=" + LastName);
        }
    }
}
